package com.tomecode.tools.maven.settings.gui.utils;

import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

import com.tomecode.tools.maven.settings.config.Maven;
import com.tomecode.tools.maven.settings.gui.FrmListMavens;
import com.tomecode.tools.maven.settings.model.Mirror;
import com.tomecode.tools.maven.settings.model.PluginRepository;
import com.tomecode.tools.maven.settings.model.Profile;
import com.tomecode.tools.maven.settings.model.Proxy;
import com.tomecode.tools.maven.settings.model.Repository;
import com.tomecode.tools.maven.settings.model.Server;

/**
 * 
 * Utilities for gui:)
 * 
 * @author Tomas Frastia
 * @see http://www.tomecode.com or
 *      http://code.google.com/p/maven-settings-editor/
 * 
 */
public final class GuiUtils {

	private static String OS_NAME;
	private static boolean windows = false;
	private static boolean linux = false;

	static {
		if (OS_NAME == null) {
			OS_NAME = System.getProperty("os.name");
			if (OS_NAME.toLowerCase().contains("win")) {
				windows = true;
			} else if (OS_NAME.toLowerCase().contains("linux")) {
				linux = true;
			}
		}

	}

	private GuiUtils() {

	}

	public static final void fillTableItem(Table table, List<Server> servers) {
		table.removeAll();
		for (Server server : servers) {
			TableItem tableItem = new TableItem(table, SWT.SINGLE);
			tableItem.setText(new String[] { server.getId(), server.getUsername(), server.getPassword() });
		}

	}

	public final static String formateNull(String value) {
		if (value == null) {
			return "";
		}
		return value;
	}

	public static final void fillTableMirrorsItem(Table table, List<Mirror> mirrors) {
		table.removeAll();
		for (Mirror mirror : mirrors) {
			TableItem tableItem = new TableItem(table, SWT.SINGLE);
			tableItem.setText(new String[] { mirror.getId(), mirror.getName(), mirror.getUrl(), mirror.getMirrorOf() });
		}

	}

	public static final void fillTableProxiesItem(Table table, List<Proxy> proxies) {
		table.removeAll();
		for (Proxy proxy : proxies) {
			TableItem tableItem = new TableItem(table, SWT.SINGLE);
			tableItem.setText(new String[] { proxy.getId(), proxy.getUsername(), proxy.getPassword(), proxy.getProtocol(), proxy.getHost(), proxy.getPort(), String.valueOf(proxy.isActive()) });
		}
	}

	public static final void fillListItem(org.eclipse.swt.widgets.List list, List<String> nonProxyHosts) {
		list.removeAll();
		for (String host : nonProxyHosts) {
			list.add(host);
		}
	}

	public static final void fillListProfileItem(org.eclipse.swt.widgets.List listProfiles, List<Profile> profiles) {
		listProfiles.removeAll();
		for (Profile profile : profiles) {
			listProfiles.add(profile.getId());
		}
	}

	public static final void fillTablePropertiesItem(Table table, Properties properties) {
		table.removeAll();
		Enumeration<Object> e = properties.keys();
		while (e.hasMoreElements()) {
			TableItem tableItem = new TableItem(table, SWT.SINGLE);
			String key = GuiUtils.formateNull((String) e.nextElement());
			String value = properties.getProperty(key);
			tableItem.setText(new String[] { key, GuiUtils.formateNull(value) });
		}
	}

	public static final void fillTableRepositoriesItem(Table table, List<Repository> repositories) {
		table.removeAll();
		for (Repository repository : repositories) {
			TableItem tableItem = new TableItem(table, SWT.SINGLE);
			String layout = repository.getLayout() == null ? "" : repository.getLayout().toString();
			tableItem.setText(new String[] { repository.getId(), repository.getName(), repository.getUrl(), layout });
		}
	}

	public static final void fillTablePluginRepositoriesItem(Table table, List<PluginRepository> pluginRepositories) {
		table.removeAll();
		for (PluginRepository repository : pluginRepositories) {
			TableItem tableItem = new TableItem(table, SWT.SINGLE);
			String layout = repository.getLayout() == null ? "" : repository.getLayout().toString();
			tableItem.setText(new String[] { repository.getId(), repository.getName(), repository.getUrl(), layout });
		}
	}

	/**
	 * fill table in {@link FrmListMavens}
	 * 
	 * @param tableMavens
	 * @param mavens
	 */
	public final static void filltableMavens(Table tableMavens, List<Maven> mavens) {
		tableMavens.removeAll();
		for (Maven maven : mavens) {
			TableItem item = new TableItem(tableMavens, SWT.SINGLE);
			item.setText(new String[] { maven.getVersion().getName(), maven.getHome().getPath(), maven.getRepository().getPath() });
			item.setChecked(maven.isActive());
		}
	}

	public static final boolean isWindows() {
		return windows;
	}

	public static final boolean isLinux() {
		return linux;
	}
}
